package com.stlm2.dems.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.DdmGoodsDao;
import com.stlm2.dems.dao.GoodsCategoryDao;
import com.stlm2.dems.dao.GoodsRelateCategoryDao;
import com.stlm2.dems.entity.*;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.util.Convert;
import com.stlm2.util.StringUtils;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("goodsCategoryService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class GoodsCategoryService extends BaseServiceImpl<GoodsCategory> {

	@Resource
	private GoodsCategoryDao goodsCategoryDao;
	@Resource
	private UserDao userDao;
	@Resource
	private GoodsRelateCategoryDao goodsRelateCategoryDao;
	@Resource
	private DdmGoodsDao ddmGoodsDao;

	@Override
	protected BaseDao <GoodsCategory> getDao() {
		return goodsCategoryDao;
	}


	public List<ModelMap> convertList(List<GoodsCategory> goodsCategoryList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (goodsCategoryList != null && goodsCategoryList.size() > 0) {
			for (GoodsCategory goodsCategory : goodsCategoryList) {
				ModelMap map = buildModelList(goodsCategory);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelList(GoodsCategory goodsCategory) {
		ModelMap model = new ModelMap();
		model.put("id",goodsCategory.getId());
		model.put("name",goodsCategory.getName());
		model.put("imgUrl",goodsCategory.getImgUrl());
		model.put("linkUrl",goodsCategory.getLinkUrl()!=null?goodsCategory.getLinkUrl():"#");
		return  model;
	}

	/**
	 * 获取一级分类
	 * ffl
	 * 2017-10-17 14:57:03
	 * @return
	 */
	public String getOneLevelCategory(){
		List<GoodsCategory> goodsCategoryList=goodsCategoryDao.getByLevel(GoodsCategoryLevel.Level1.getIntValue());
		return CodeContent.getModelByCode(1,"",convert_s(goodsCategoryList));
	}

	/**
	 * 根据一级分类获取二级分类及三级分类
	 * ffl
	 * 2017-10-17 14:57:03
	 * @return
	 */
	public String getByOneLevelCategory(String parentCategoryId){
		if(!NumberUtils.isDigits(parentCategoryId)){
			return CodeContent.getModelByCode(0,"未传入一级分类id",null);
		}
		GoodsCategory goodsCategory=goodsCategoryDao.getById(NumberUtils.toInt(parentCategoryId));
		if(goodsCategory==null){
			return CodeContent.getModelByCode(0,"该分类不存在",null);
		}
		ModelMap model=new ModelMap();
		model.put("imgUrl",goodsCategory.getImgUrl());
		model.put("linkUrl",goodsCategory.getLinkUrl()!=null?goodsCategory.getLinkUrl():"#");
		List<GoodsCategory> goodsCategoryList=goodsCategoryDao.getByParentCategoryId(NumberUtils.toInt(parentCategoryId));
		model.put("secondCategory",convert_levelTwo(goodsCategoryList));
		return CodeContent.getModelByCode(1,"",model);
	}

	/**
	 * 根据二级分类获取三级分类
	 * ffl
	 * 2017-10-17 14:57:03
	 * @return
	 */
	public String getByTwoLevelCategory(String parentCategoryId){
		if(!NumberUtils.isDigits(parentCategoryId)){
			return CodeContent.getModelByCode(0,"未传入二级分类id",null);
		}
		List<GoodsCategory> goodsCategoryList=goodsCategoryDao.getByParentCategoryId(NumberUtils.toInt(parentCategoryId));
		return CodeContent.getModelByCode(1,"",convert_levelThree(goodsCategoryList));
	}


	/**
	 * 获取 热门品牌  级别为二级分类
	 * ffl
	 * 2017-10-19 11:54:58
	 * @return
	 */
	public String getByTypeAndLevel(){
		//获取 热门品牌  级别为二级分类
		List<GoodsCategory> goodsCategoryList=goodsCategoryDao.getByTypeAndLevel(2,2);
		return CodeContent.getModelByCode(1,"",convert_levelTwo(goodsCategoryList));
	}
	//精简二级分类数据
	public List<ModelMap> convert_two(List<GoodsCategory> goodsCategoryList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (goodsCategoryList != null && goodsCategoryList.size() > 0) {
			for (GoodsCategory goodsCategory : goodsCategoryList) {
				ModelMap map = buildModel_two(goodsCategory);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel_two(GoodsCategory goodsCategory) {
		ModelMap model = new ModelMap();
		model.put("id",goodsCategory.getId());
		model.put("name",goodsCategory.getName());
		model.put("level",goodsCategory.getGoodsCategoryLevel()!=null?goodsCategory.getGoodsCategoryLevel().toString():"");
		model.put("levelValue",goodsCategory.getGoodsCategoryLevel()!=null?goodsCategory.getGoodsCategoryLevelValue():"");
		List<GoodsCategory> goodsCategoryList=goodsCategoryDao.getByParentCategoryId(goodsCategory.getId());
		model.put("categoryType",goodsCategory.getCategoryType());
		model.put("thirdCategory",convert_levelThree(goodsCategoryList));
		return  model;
	}

	/**
	 * 前台 分类展示
	 * ffl
	 * 2017年10月12日15:42:09
	 * @return
	 */
	public String disPlayCategory(){
		List<GoodsCategory> goodsCategoryList=goodsCategoryDao.getByLevel(GoodsCategoryLevel.Level1.getIntValue());
		return CodeContent.getModelByCode(1,"",convert_levelOne(goodsCategoryList));
	}


	//精简一级分类数据
	public List<ModelMap> convert_levelOne(List<GoodsCategory> goodsCategoryList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (goodsCategoryList != null && goodsCategoryList.size() > 0) {
			for (GoodsCategory goodsCategory : goodsCategoryList) {
				ModelMap map = buildModel_levelOne(goodsCategory);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel_levelOne(GoodsCategory goodsCategory) {
		ModelMap model = new ModelMap();
		model.put("id",goodsCategory.getId());
		GoodsRelateCategory goodsRelateCategory=goodsRelateCategoryDao.getByGoodsCategoryId(goodsCategory.getId());
		if(goodsRelateCategory!=null){
			model.put("goodsId",goodsRelateCategory.getGoodsId());
			DdmGoods goods=ddmGoodsDao.getById(goodsRelateCategory.getGoodsId());
			if(goods!=null){
				model.put("goodsImg",goods.getMainImg());
			}else {
				model.put("goodsImg","http://image.chinaddcat.com/category.jpg");
			}
		}else {
			model.put("goodsId",1);
			model.put("goodsImg","http://image.chinaddcat.com/category.jpg");
		}
		model.put("name",goodsCategory.getName());
		model.put("level",goodsCategory.getGoodsCategoryLevel()!=null?goodsCategory.getGoodsCategoryLevel().toString():"");
		model.put("levelValue",goodsCategory.getGoodsCategoryLevel()!=null?goodsCategory.getGoodsCategoryLevelValue():"");
		List<GoodsCategory> goodsCategoryList=new ArrayList<GoodsCategory>();
		if(goodsCategoryDao.getByParentCategoryId(goodsCategory.getId())!=null)
			goodsCategoryList=goodsCategoryDao.getByParentCategoryId(goodsCategory.getId());
		model.put("secondCategory",convert_levelTwo(goodsCategoryList));
		return  model;
	}
	//精简二级分类数据
	public List<ModelMap> convert_levelTwo(List<GoodsCategory> goodsCategoryList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (goodsCategoryList != null && goodsCategoryList.size() > 0) {
			for (GoodsCategory goodsCategory : goodsCategoryList) {
				ModelMap map = buildModel_levelTwo(goodsCategory);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel_levelTwo(GoodsCategory goodsCategory) {
		ModelMap model = new ModelMap();
		model.put("id",goodsCategory.getId());
		model.put("name",goodsCategory.getName());
		model.put("level",goodsCategory.getGoodsCategoryLevel()!=null?goodsCategory.getGoodsCategoryLevel().toString():"");
		model.put("levelValue",goodsCategory.getGoodsCategoryLevel()!=null?goodsCategory.getGoodsCategoryLevelValue():"");
		List<GoodsCategory> goodsCategoryList=goodsCategoryDao.getByParentCategoryId(goodsCategory.getId());
		model.put("categoryType",goodsCategory.getCategoryType());
		model.put("thirdCategory",convert_levelThree(goodsCategoryList));
		return  model;
	}
	//精简三级分类数据
	public List<ModelMap> convert_levelThree(List<GoodsCategory> goodsCategoryList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (goodsCategoryList != null && goodsCategoryList.size() > 0) {
			for (GoodsCategory goodsCategory : goodsCategoryList) {
				ModelMap map = buildModel_levelThree(goodsCategory);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel_levelThree(GoodsCategory goodsCategory) {
		ModelMap model = new ModelMap();
		model.put("id",goodsCategory.getId());
		model.put("name",goodsCategory.getName());
		model.put("level",goodsCategory.getGoodsCategoryLevel()!=null?goodsCategory.getGoodsCategoryLevel().toString():"");
		model.put("levelValue",goodsCategory.getGoodsCategoryLevel()!=null?goodsCategory.getGoodsCategoryLevelValue():"");
		model.put("imgUrl",goodsCategory.getImgUrl()!=null?goodsCategory.getImgUrl():"");
		return  model;
	}














	//   crm     +++++++++++++++++++++++++++++++++     crm

	/**
	 * 添加 商品分类
	 * ffl
	 * 2017-10-13 14:52:58
	 * @param token
	 * @param name 分类名称
	 * @param summary 分类简介
	 * @param level  分类等级
	 * @param imgUrl logo图片路径
	 * @param parentCategoryId 父分类id
	 * @param sort  优先级
	 * @param categoryType 分类 类型  1普通分类 2热门品牌
	 * @return
	 */
	public String AddCategory(String token,String name, String summary, String level,String imgUrl,String  parentCategoryId, String sort,String categoryType,String linkUrl){
         User user=userDao.getByToken(token);
         if(user==null)
         	return CodeContent.getModelByCode(-1,"",null);
         if(!NumberUtils.isDigits(level)){
         	return CodeContent.getModelByCode(0,"请指定商品分类等级",null);
		 }
		 int levelValue=NumberUtils.toInt(level);
         if (com.stlm2.util.StringUtils.isNullOrEmpty(name)){
         	return CodeContent.getModelByCode(0,"商品分类名称不能为空",null);
		 }
		if (com.stlm2.util.StringUtils.isNullOrEmpty(summary)){
			return CodeContent.getModelByCode(0,"商品分类简介不能为空",null);
		}
         if(!NumberUtils.isDigits(sort)){
			 return CodeContent.getModelByCode(0,"请指定商品分类优先级",null);
		 }


		GoodsCategory goodsCategory=new GoodsCategory();
		 if(levelValue==1){
				//父分类为空   必须添加跳转路径
			 GoodsCategory oldOne=goodsCategoryDao.getByName(name);
			 if(oldOne!=null){
				 return CodeContent.getModelByCode(0,"该商品分类已经添加，请勿重复添加",null);
			 }
			 if(StringUtils.isNullOrEmpty(linkUrl)){
				 return CodeContent.getModelByCode(0,"一级分类需要添加跳转路径",null);
			 }
			 goodsCategory.setLinkUrl(linkUrl);
			 goodsCategory.setCategoryType(1);
		 }else if(levelValue==2){
			 if(!NumberUtils.isDigits(categoryType)){
				 return CodeContent.getModelByCode(0,"请指定商品分类类型",null);
			 }
			 goodsCategory.setCategoryType(NumberUtils.toInt(categoryType));
			 //父分类不为空   不需要logo 图片
			 if (!NumberUtils.isDigits(parentCategoryId)){
				 return CodeContent.getModelByCode(0,"请指定商品分类父分类id",null);
			 }
			 GoodsCategory oldOne=goodsCategoryDao.getByNameAndParentCategoryId(name,NumberUtils.toInt(parentCategoryId));
			 if(oldOne!=null){
				 return CodeContent.getModelByCode(0,"该商品分类已经添加，请勿重复添加",null);
			 }
			 goodsCategory.setParentCategoryId(NumberUtils.toInt(parentCategoryId));
		 }else if(levelValue==3){
			 //父分类不为空
			 if (!NumberUtils.isDigits(parentCategoryId)){
				 return CodeContent.getModelByCode(0,"请指定商品分类父分类id",null);
			 }
			 GoodsCategory oldOne=goodsCategoryDao.getByNameAndParentCategoryId(name,NumberUtils.toInt(parentCategoryId));
			 if(oldOne!=null){
				 return CodeContent.getModelByCode(0,"该商品分类已经添加，请勿重复添加",null);
			 }
			 goodsCategory.setParentCategoryId(NumberUtils.toInt(parentCategoryId));
			 goodsCategory.setCategoryType(1);

		 }else {
		 	return CodeContent.getModelByCode(0,"商品分类等级不符合要求",null);
		 }
		if(com.stlm2.util.StringUtils.isNullOrEmpty(imgUrl)){
			return CodeContent.getModelByCode(0,"商品分类图片不能为空",null);
		}
		goodsCategory.setImgUrl(imgUrl);
		goodsCategory.setName(name);
		goodsCategory.setSummary(summary);
		goodsCategory.setGoodsCategoryLevel(GoodsCategoryLevel.getByIntValue(levelValue));
		goodsCategory.setSort(NumberUtils.toInt(sort));
		goodsCategory.setIsValid(true);
		goodsCategory.setCreateUserId(user.getId());
		goodsCategory.setCreateTime(new Date());
        goodsCategoryDao.add(goodsCategory);
		return CodeContent.getModelByCode(1,"",null);
	}


	/**
	 * 编辑商品分类
	 * ffl
	 * 2017-10-13 17:36:15
	 * @param token
	 * @param categoryId 商品分类id
	 * @param name 名称
	 * @param summary 简介
	 * @param imgUrl logo图片路径
	 * @param sort 优先级
	 * @param categoryType 分类类型  1普通分类 2特殊分类
	 * @param isValid  是否有效  1有效  0无效
	 * @return
	 */
	public  String editCategory(String token,String  categoryId, String name, String summary, String imgUrl, String sort,String categoryType,String isValid,String linkUrl){
		User user=userDao.getByToken(token);
		if(user==null)
			return CodeContent.getModelByCode(-1,"",null);
		if (!NumberUtils.isDigits(categoryId)){
			return CodeContent.getModelByCode(0,"请指定编辑的商品分类",null);
		}
		GoodsCategory goodsCategory=goodsCategoryDao.getById(NumberUtils.toInt(categoryId));
		if (goodsCategory==null){
			return CodeContent.getModelByCode(0,"要编辑的商品分类未找到",null);
		}
		if (!com.stlm2.util.StringUtils.isNullOrEmpty(summary)){
			goodsCategory.setSummary(summary);
		}
		if (!com.stlm2.util.StringUtils.isNullOrEmpty(imgUrl)){
			goodsCategory.setImgUrl(imgUrl);
		}
		if(NumberUtils.isDigits(categoryType)){
			goodsCategory.setCategoryType(NumberUtils.toInt(categoryType));
		}

		if(NumberUtils.isDigits(sort)) {
			goodsCategory.setSort(NumberUtils.toInt(sort));
		}
		if (!com.stlm2.util.StringUtils.isNullOrEmpty(linkUrl)){
			goodsCategory.setLinkUrl(linkUrl);
		}
		Boolean valid=false;
		if(!com.stlm2.util.StringUtils.isNullOrEmpty(isValid)){
			if ("true".equals(isValid)){
				valid=true;
			}

		}else {
			return CodeContent.getModelByCode(0,"请指定要编辑的商品分类是否有效",null);
		}


        //根据编辑前 分类是否有效  核实修改会不会造成 分类重复
        if(goodsCategory.getIsValid()){//原本有效
			if(valid){//不改变继续有效  检查 是否修改名称
				if(!com.stlm2.util.StringUtils.isNullOrEmpty(name)){
					if(!name.equals(goodsCategory.getName())){//修改名称 核实是否重复
						if(GoodsCategoryLevel.Level1.equals(goodsCategory.getGoodsCategoryLevel())){
							GoodsCategory oldOne=goodsCategoryDao.getByName(name);
							if(oldOne!=null){
								return CodeContent.getModelByCode(0,"要编辑的商品分类的新名称已存在",null);
							}
						}else {
							GoodsCategory oldOne=goodsCategoryDao.getByNameAndParentCategoryId(name,goodsCategory.getParentCategoryId());
							if(oldOne!=null){
								return CodeContent.getModelByCode(0,"要编辑的商品分类的新名称已存在",null);
							}
						}
						goodsCategory.setName(name);
					}
				}
			}else {//变无效
				if(!com.stlm2.util.StringUtils.isNullOrEmpty(name)){
					goodsCategory.setName(name);
				}

			}

		}else {//原本无效
			if (valid){//变有效
				if(!com.stlm2.util.StringUtils.isNullOrEmpty(name)){
					if(!name.equals(goodsCategory.getName())){//修改名称 核实是否重复
						if(GoodsCategoryLevel.Level1.equals(goodsCategory.getGoodsCategoryLevel())){
							GoodsCategory oldOne=goodsCategoryDao.getByName(name);
							if(oldOne!=null){
								return CodeContent.getModelByCode(0,"要编辑的商品分类的新名称已存在",null);
							}
						}else {//二级或三级分类
							GoodsCategory oldOne=goodsCategoryDao.getByNameAndParentCategoryId(name,goodsCategory.getParentCategoryId());
							if(oldOne!=null){
								return CodeContent.getModelByCode(0,"要编辑的商品分类的新名称已存在",null);
							}
						}
						goodsCategory.setName(name);
					}else {//查看未修改的名称 是否重复
						if(GoodsCategoryLevel.Level1.equals(goodsCategory.getGoodsCategoryLevel())){
							GoodsCategory oldOne=goodsCategoryDao.getByName(goodsCategory.getName());
							if(oldOne!=null){
								return CodeContent.getModelByCode(0,"要编辑的商品分类的名称已存在",null);
							}
						}else {
							GoodsCategory oldOne=goodsCategoryDao.getByNameAndParentCategoryId(goodsCategory.getName(),goodsCategory.getParentCategoryId());
							if(oldOne!=null){
								return CodeContent.getModelByCode(0,"要编辑的商品分类的名称已存在",null);
							}
						}
					}
				}else {//不修改名称
					if(GoodsCategoryLevel.Level1.equals(goodsCategory.getGoodsCategoryLevel())){
						GoodsCategory oldOne=goodsCategoryDao.getByName(goodsCategory.getName());
						if(oldOne!=null){
							return CodeContent.getModelByCode(0,"要编辑的商品分类的名称已存在",null);
						}
					}else {
						GoodsCategory oldOne=goodsCategoryDao.getByNameAndParentCategoryId(goodsCategory.getName(),goodsCategory.getParentCategoryId());
						if(oldOne!=null){
							return CodeContent.getModelByCode(0,"要编辑的商品分类的名称已存在",null);
						}
					}
				}
			}else {//继续无效
				if(!com.stlm2.util.StringUtils.isNullOrEmpty(name)){
					goodsCategory.setName(name);
				}
			}

		}

		goodsCategory.setIsValid(valid);
		goodsCategoryDao.modify(goodsCategory);
		return CodeContent.getModelByCode(1,"",null);
	}


	/**
	 * 分类查询
	 * ffl
	 * 2017-10-16 10:48:45
	 * @param token
	 * @param level
	 * @param name
	 * @param parentCategoryId
	 * @param isValid
	 * @param categoryType 分类类型  1普通分类 2特殊分类
	 * @param pageNu 第几页
	 * @return
	 */
	public String searchCategory(String token , String level ,String name , String parentCategoryId,String isValid,String categoryType,String pageNu){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		GoodsCategory  param=new GoodsCategory();
		if(NumberUtils.isDigits(level)){
			param.setGoodsCategoryLevelValue(NumberUtils.toInt(level));
		}
		if(NumberUtils.isDigits(categoryType)){
			param.setCategoryType(NumberUtils.toInt(categoryType));
		}
		if(!StringUtils.isNullOrEmpty(name)){
			param.setName(name);
		}
		if(NumberUtils.isDigits(parentCategoryId)){
			param.setParentCategoryId(NumberUtils.toInt(parentCategoryId));
		}
		if(NumberUtils.isDigits(isValid)){
			if(NumberUtils.toInt(isValid)==1){
				param.setIsValid(true);
			}else {
				param.setIsValid(false);
			}
		}
		int pageNo=1;
		if (NumberUtils.isDigits(pageNu)){
			pageNo=NumberUtils.toInt(pageNu);
		}else {
			return CodeContent.getModelByCode(0,"指定查询页数",null);
		}

		PageView pageView=goodsCategoryDao.queryPageView(pageNo,10,param);
		ModelMap model=new ModelMap();
		if (pageView!=null){
			model.put("categoryList",convert(pageView.getRecords()));
			model.put("recordsTotal", pageView.getRowCount());
			model.put("totalPage", pageView.getPageCount());
		}
		model.put("pageNo", pageNo);
		return CodeContent.getModelByCode(1,"",model);
	}


	public List<ModelMap> convert(List<GoodsCategory> goodsCategoryList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (goodsCategoryList != null && goodsCategoryList.size() > 0) {
			for (GoodsCategory goodsCategory : goodsCategoryList) {
				ModelMap map = buildModel(goodsCategory);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel(GoodsCategory goodsCategory) {
		ModelMap model = new ModelMap();
		if(goodsCategory!=null) {
			model.put("id", goodsCategory.getId());
			model.put("name", goodsCategory.getName());
			model.put("level",goodsCategory.getGoodsCategoryLevel()!=null?goodsCategory.getGoodsCategoryLevel().toString():"");
			model.put("levelValue",goodsCategory.getGoodsCategoryLevelValue()!=null?goodsCategory.getGoodsCategoryLevelValue():"");
			model.put("summary",goodsCategory.getSummary()!=null?goodsCategory.getSummary():"");
			GoodsCategory parentCategory=null;
			if(goodsCategory.getParentCategoryId()!=null){
				 parentCategory=goodsCategoryDao.getById(goodsCategory.getParentCategoryId());
			}
			if(parentCategory!=null){
				model.put("parentCategoryName",parentCategory.getName()!=null?parentCategory.getName():"");
			}else {
				model.put("parentCategoryName","");
			}
			User user=userDao.getById(goodsCategory.getCreateUserId());
			if(user!=null){
				model.put("createUserName",user.getRealName());
			}else {
				model.put("createUserName","");
			}
			model.put("createTime", Convert.toFullYMDHMS(goodsCategory.getCreateTime()));
			if(goodsCategory.getCategoryType().equals(1)){
				model.put("categoryType","普通分类");
			}else {
				model.put("categoryType","特殊分类");
			}
			if (goodsCategory.getIsValid()!=null &&goodsCategory.getIsValid()){
				model.put("isValid","有效");
			}else {
				model.put("isValid","无效");
			}

			model.put("sort",goodsCategory.getSort());
		}
		return  model;
	}

	/**获取一级分类
	 * ffl
	 * 2017-10-18 09:42:51
	 * @return
	 */
	public String getFirstLevelCategory(){
		List<GoodsCategory> goodsCategoryList=goodsCategoryDao.getByLevel(GoodsCategoryLevel.Level1.getIntValue());
		return CodeContent.getModelByCode(1,"",convert_s(goodsCategoryList));
	}

	public List<ModelMap> convert_s(List<GoodsCategory> goodsCategoryList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (goodsCategoryList != null && goodsCategoryList.size() > 0) {
			for (GoodsCategory goodsCategory : goodsCategoryList) {
				ModelMap map = buildModel_s(goodsCategory);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModel_s(GoodsCategory goodsCategory) {
		ModelMap model = new ModelMap();
		model.put("id",goodsCategory.getId());
		model.put("name",goodsCategory.getName());
		return  model;
	}

	/**
	 * 根据一级分类获取二级分类
	 * ffl
	 * 2017-10-17 14:57:03
	 * @return
	 */
	public String getByFirstLevelCategory(String parentCategoryId){
		if(!NumberUtils.isDigits(parentCategoryId)){
			return CodeContent.getModelByCode(0,"未传入一级分类id",null);
		}
		List<GoodsCategory> goodsCategoryList=goodsCategoryDao.getByParentCategoryId(NumberUtils.toInt(parentCategoryId));
		return CodeContent.getModelByCode(1,"",convert_s(goodsCategoryList));
	}

	/**
	 * 根据id获取分类 详情
	 * ffl
	 * 2017-10-18 10:10:02
	 * @param token
	 * @param id
	 * @return
	 */
	public String getByIdForDetail(String token ,String id){
		User user=userDao.getByToken(token);
		if(user==null){
			return CodeContent.getModelByCode(-1,"",null);
		}
		if(!NumberUtils.isDigits(id)){
			return CodeContent.getModelByCode(0,"未传要查看的分类id",null);
		}
		GoodsCategory goodsCategory=goodsCategoryDao.getById(NumberUtils.toInt(id));
		ModelMap model=new ModelMap();
		model.put("goodsCategory",buildModel_detail(goodsCategory));
		return CodeContent.getModelByCode(1,"",model);
	}

	public ModelMap buildModel_detail(GoodsCategory goodsCategory) {
		ModelMap model = new ModelMap();
		if(goodsCategory!=null) {
			model.put("id", goodsCategory.getId());
			model.put("name", goodsCategory.getName());
			model.put("level",goodsCategory.getGoodsCategoryLevel()!=null?goodsCategory.getGoodsCategoryLevel().toString():"");
			model.put("levelValue",goodsCategory.getGoodsCategoryLevelValue()!=null?goodsCategory.getGoodsCategoryLevelValue():"");
			model.put("summary",goodsCategory.getSummary()!=null?goodsCategory.getSummary():"");
			model.put("createTime",Convert.toFullYMDHMS(goodsCategory.getCreateTime()));
			GoodsCategory parentCategory=null;
			if(goodsCategory.getParentCategoryId()!=null){
				parentCategory=goodsCategoryDao.getById(goodsCategory.getParentCategoryId());
			}
			if(parentCategory!=null){
				model.put("parentCategoryName",parentCategory.getName()!=null?parentCategory.getName():"");
			}else {
				model.put("parentCategoryName","");
			}
			model.put("imgUrl",goodsCategory.getImgUrl()!=null?goodsCategory.getImgUrl():"");
			User user=userDao.getById(goodsCategory.getCreateUserId());
			if(user!=null){
				model.put("createUserName",user.getRealName());
			}else {
				model.put("createUserName","");
			}
			model.put("createTime", Convert.toFullYMDHMS(goodsCategory.getCreateTime()));
			if(goodsCategory.getCategoryType().equals(1)){
				model.put("categoryType","普通分类");
			}else {
				model.put("categoryType","特殊分类");
			}
			model.put("categoryTypeValue",goodsCategory.getCategoryType());
			model.put("isValid",goodsCategory.getIsValid());
			model.put("linkUrl",goodsCategory.getLinkUrl()!=null?goodsCategory.getLinkUrl():"");
			model.put("sort",goodsCategory.getSort());
		}
		return  model;
	}

}